﻿using System.Collections.Immutable;
using log4net.Repository.Hierarchy;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using MySqlX.XDevAPI;
using WebApplication11.Dao;
using WebApplication11.Models;
using WebApplication11.Services;

namespace WebApplication9.Controllers;

public class adminController : Controller
{
    private adminService _adminService = new adminService();
    private departmentDao _departmentDao = new departmentDao();
    private ILogger<adminController> _logger;
    public bool Auth()
    {
        if (HttpContext.Session.GetInt32("Status") == 1)
        {
            return true;
        }
        return false;
    }
    // GET
    public IActionResult Index()
    {
        if (!Auth())
        {
            return RedirectToAction("Index", "Home");
        }

        if (HttpContext.Session.GetString("searching") != null)
        {
            var a = HttpContext.Session.GetString("searching");
            ViewBag.assetss = _adminService.search(HttpContext.Session.GetString("searching"));
            HttpContext.Session.Remove("searching");
        }
        else
        {
            if (HttpContext.Session.GetString("private") != "1")
            {
                ViewBag.assetss = _adminService.SelectAll();
            }
            else
            {
                ViewBag.assetss = _adminService.Select(HttpContext.Session.GetString("user"));
            }
        }
        if (HttpContext.Session.GetInt32("Status") == 1)
        {
            ViewBag.Status = 1;
            ViewBag.username = HttpContext.Session.GetString("user");
        }
        if (HttpContext.Session.GetInt32("Insert") != 1)
        {
            ViewBag.Insert = 0;
        }
        if (ViewBag.assetss != null)
        {
            ViewBag._max = getMax() + 1;
        }
        else
        {
            ViewBag._max = -1;
        }

        var count = getIdentity();
        if (count != 0)
        {
            ViewBag.Identity = count;
        }
        else
        {
            ViewBag.Identity = 0;
        }
        return View();
    }
    
    public IActionResult showMine()
    {
        HttpContext.Session.SetString("private", "1");
        return RedirectToAction("Index");
    }

    public IActionResult showAll()
    {
        HttpContext.Session.Remove("private");
        HttpContext.Session.Remove("searching");
        return RedirectToAction("Index");
    }
    
    public bool Deleteassets(int id) {
        return _adminService.Delete(id);
    }
    
    public ActionResult Updateassets(message message) {
        if (!Auth()) {
            return RedirectToAction("Index", "Home");
        }
        HttpContext.Session.SetString("Alert", "删除" + (_adminService.Update(message) ? "成功" : "失败"));
        return RedirectToAction("Index");
    }
    
    public ActionResult addNew(message message)
    {
        ViewBag.Insert = 0;
        HttpContext.Session.Remove("Insert");
        _adminService.Insert(message);
        return RedirectToAction("Index");
    }
    
    public ActionResult insertNewAssets(message message)
    {
        ViewBag.Insert = 1;
        HttpContext.Session.SetInt32("Insert", 1);
        return RedirectToAction("Index");
    }
    
    public int getMax()
    {
        int _max = 0;
        for (int i = 0; i < ViewBag.assetss.Count; i++)
        {
            if (_max < ViewBag.assetss[i].Id)
            {
                _max = ViewBag.assetss[i].Id;
            }
        }
        return _max;
    }

    public ActionResult Search(string msg)
    {
        HttpContext.Session.SetString("searching", msg);
        return RedirectToAction("Index");
    }

    public int getIdentity()
    {
        var res = _adminService.getIdentity(HttpContext.Session.GetString("user"));
        return res.Count;
    }
}